//
//  HWGamexiangQingTableViewCell.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/6.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWGamexiangQingTableViewCell.h"
#import "HWJietuCollectionViewCell.h"
#import "HWGameJiamJieCell.h" // 简介的Cell
#import "HWFuliTableViewCell.h" // 福利Cell
#import "HWYouxiLiBaoTableViewCell.h" //  游戏礼包
#import "HWXiHuanTableViewCell.h" //  游戏喜欢
#import "HWpinglunweixingView.h" //评论UIview

#import "HWyouxixiangqinglimiandepinglunView.h"

#import "HWfulikongzhqiViewController.h"




@interface HWGamexiangQingTableViewCell()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property(strong,nonatomic) UITableView *maintablview;
@property(strong,nonatomic) UIView *hederView;
@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSArray *sectinArr;
@property(strong,nonatomic) UIScrollView *scrollView; //

@property(strong,nonatomic) UIView *youxipingbiatijiaView; // 游戏评论标题
@property(strong,nonatomic)UILabel *youxipingjianLabel;
 


@property(strong,nonatomic) HWpinglunweixingView *pingluanshituView; //



@end

@implementation HWGamexiangQingTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
        // 设置Tabview的hederView
        [self addHederView];
    
    }
    
    return  self;
}




- (void)setUI {

   // self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  1000) style:UITableViewStylePlain];
    self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  5200) style:UITableViewStylePlain];
    self.maintablview.delegate = self;
    self.maintablview.dataSource = self;
    self.maintablview.tag = 10;
    self.maintablview.scrollEnabled = NO;
    self.maintablview.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.maintablview.tableFooterView = [[UIView alloc]init];
    self.maintablview.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.maintablview];
    
    
    
    self.pingluanshituView = [[HWpinglunweixingView alloc]initWithFrame:CGRectMake(0, self.maintablview.bottom, kScreenW, self.gaodutablViewH)];
    self.pingluanshituView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.pingluanshituView];
    
    self.youxipingbiatijiaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 40)];
    self.youxipingbiatijiaView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.youxipingbiatijiaView];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,10, kScreenW-80, 20)];
               //     nameLabel.text = fanlilistArr[i];
        //  nameLabel.backgroundColor =  RGB(248, 119, 6);
         nameLabel.backgroundColor = [UIColor clearColor];
          nameLabel.textAlignment = NSTextAlignmentLeft;
          nameLabel.font = [UIFont systemFontOfSize:14];
          nameLabel.textColor = RGB(68, 68, 68);
     //     nameLabel.layer.cornerRadius = 8;
     //     nameLabel.layer.masksToBounds = YES;
          nameLabel.text = @"游戏评价";
       nameLabel.text =  [HWjianzhiduiObject keylocalization:@"游戏评价"];
    
    
      [self.youxipingbiatijiaView addSubview:nameLabel];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kScreenW-100, 0, 100, 40);
    [button setTitleColor:RGB(246, 106, 5) forState:UIControlStateNormal];
    [button setTitle:@"新增评论" forState:UIControlStateNormal];
    
    [button setTitle:[HWjianzhiduiObject keylocalization:@"新增评论"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
     [self.youxipingbiatijiaView addSubview:button];
    [button addTarget:self action:@selector(pinglunbutton) forControlEvents:UIControlEventTouchUpInside];
    
    
    

     
    
    
}

- (void)setDataStr:(NSString *)dataStr {
    
    
    _dataStr = dataStr;
    
  //   self.sectinArr = @[@"游戏简介",@"福利",@"游戏礼包",@"猜你喜欢"];
    
    
    self.sectinArr  =@[[HWjianzhiduiObject keylocalization:@"游戏简介"],[HWjianzhiduiObject keylocalization:@"福利"],[HWjianzhiduiObject keylocalization:@"游戏礼包"],[HWjianzhiduiObject keylocalization:@"猜你喜欢"]];
    
    
    [self.maincollectionView reloadData];
    
    [self.maintablview reloadData];
    
      NSString *str = self.dataDic[@"content"];
          
          NSArray *liaArr  = self.dataDic[@"lb"];
          
          int libao = [liaArr  count];
          
          CGFloat  contentH = [self calculateRowHeight:str fontSize:12];
          
         
          // 
        CGFloat gaodu = 0;
          if (libao == 0 ) {
               gaodu =  180*CFLScaleValue+36+10+contentH+3+75*CFLScaleValue+6+(70+6)+58*CFLScaleValue+21+6+36*4+40-20;
          } else {
              
               gaodu = 180*CFLScaleValue+36+10+contentH+3+75*CFLScaleValue+6+(70+6)*libao+58*CFLScaleValue+21+6+36*4+40-20;
              
              }

        //   gaodu = 180*CFLScaleValue+36+10+contentH+3+75*CFLScaleValue+6+(70+6)*libao+58*CFLScaleValue+21+6+36*4+40;
       
    
         self.youxipingbiatijiaView.frame = CGRectMake(0, gaodu, kScreenW, 40);
    
    
    
    
       self.pingluanshituView.frame = CGRectMake(0, gaodu+40, kScreenW, self.gaodutablViewH);
    
      self.pingluanshituView.dataMuarr = self.pinglunMuArr;
     self.pingluanshituView.mainaTableVie .frame = CGRectMake(0, 0, kScreenW, self.gaodutablViewH);
      [self.pingluanshituView shuaxingshuju];
    
    
   
    
    
    
    
    
    
                                    
    
    
    
    
}


// 设置HederView
- (void)addHederView {
    
    self.hederView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 180*CFLScaleValue+36+10)];
   // self.hederView.backgroundColor = [UIColor redColor];
    self.maintablview.tableHeaderView = self.hederView;
    
    UILabel   *youxiJietuLabel = [[UILabel alloc]initWithFrame:CGRectMake(16,  0, 300, 36)];
    youxiJietuLabel.text = @"游戏截图";
    
    youxiJietuLabel.text =  [HWjianzhiduiObject keylocalization:@"游戏截图"];
    
    youxiJietuLabel.textColor = RGB(51, 51, 51);
    youxiJietuLabel.textAlignment  = NSTextAlignmentLeft;
    youxiJietuLabel.font = [UIFont systemFontOfSize:13];
  //  youxiJietuLabel.backgroundColor = [UIColor yellowColor];
    [self.hederView addSubview:youxiJietuLabel];
    
    // 添加截图
    [self screenshotsView];
}
 // 添加截图
- (void)screenshotsView   {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 8*CFLScaleValue;
 //   layout.minimumInteritemSpacing = 30;
    
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(9, 36+4, kScreenW-9,180*CFLScaleValue) collectionViewLayout:layout];
    self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
   // self.maincollectionView.backgroundColor = RGB(217, 247, 250);
    self.maincollectionView.backgroundColor = [UIColor whiteColor];
    
    //self.maincollectionView.alwaysBounceHorizontal = YES;
    
    
  //  self.maincollectionView.contentInset = UIEdgeInsetsMake(6, 10, 6, 10);
    // [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
  //  [self.maincollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJietuCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    
    //  [UINib nibWithNibName:@"HWShouYouCollectionViewCell" bundle:nil]
    
    self.maincollectionView.userInteractionEnabled = YES;
    [self.hederView  addSubview:self.maincollectionView];
    
    
    UIView  *fengexianView = [[UIView alloc]initWithFrame:CGRectMake(18,180*CFLScaleValue+36+10-1, kScreenW-36, 1)];
    fengexianView.backgroundColor =RGB(237, 237, 237);
    [self.hederView addSubview:fengexianView];
    
    

    
}














#pragma mark ------ UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return  self.sectinArr.count;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 2) {
        
        if ([self.dataDic[@"lb"] count] > 0) {
            return  [self.dataDic[@"lb"] count];
            
        } else {
            
            return  1;
            
        }
        
    }
    
    return  1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 ) {
        
        
        static NSString   *cellID = @"cellId";
        HWGameJiamJieCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell ) {
            cell = [[HWGameJiamJieCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        cell.zhangkan = self.zhangkan;
        cell.dataStr = self.dataStr;
        cell.backgroundColor = [UIColor whiteColor];
        return  cell;
        

    } else if (indexPath.section == 1){
        
        static NSString *cellID = @"fulieCell";
    
        HWFuliTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            
            cell = [[HWFuliTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        cell.fuli_infoSt = self.dataDic[@"fuli_info"];
        return  cell;
    
        
    } else if (indexPath.section == 2) {
        
        static NSString *cellID = @"YouxiLiBao";

        HWYouxiLiBaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"HWYouxiLiBaoTableViewCell" owner:nil options:nil].lastObject;
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        if ([self.dataDic[@"lb"] count] > 0) {
            
            NSArray *arr =  (NSArray *)self.dataDic[@"lb"];
            cell.dataDic  = arr[indexPath.row];
               
        
            
        } else {
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 76)];
            label.text  = @"暂无礼包";
            label.text  =  [HWjianzhiduiObject keylocalization:@"暂无礼包"];
            
            label.textAlignment  = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor whiteColor];
            label.textColor = [UIColor redColor];
            [cell.contentView addSubview:label];
            
            
            
            
        }
        
   
        return  cell;
        
        
        
    } else if (indexPath.section == 3) {
        
        
        static NSString *cellID = @"xihuanID";
        
        HWXiHuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            
            cell =  [[HWXiHuanTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
        }
        
        cell.gameArr = self.dataDic[@"game"];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
    
        return  cell;
        

    }
    
    
    
    else  {
        
        
        static NSString *cellID = @"celiD";
        UITableViewCell *cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor whiteColor];
        return  cell;
        
        
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
  //    NSLog(@"dijioioijohiohio");
    
    if (indexPath.section == 1) {
        
         NSLog(@"dijioioijohiohio");
         
        if ([self.dataDic[@"fuli_info"] isEqualToString:@""]) {
             //   self.namelabee.text = @"暂无福利活动,敬请期待！";
          } else {
            HWfulikongzhqiViewController * fuli = [[HWfulikongzhqiViewController alloc] init];
            fuli.neironStr =  [NSString stringWithFormat:@"%@",self.dataDic[@"fuli_info"]];
             [[HWViewUtils getCurrentVC].navigationController  pushViewController:fuli animated:YES];
                         
              
          }
          
        
       
        
        
    }
    
    
    
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
      NSString *str = self.dataStr;
        
        
      CGFloat  contentH = [self calculateRowHeight:str fontSize:12];
        
      if (self.zhangkan == NO) {
          
          if (contentH > 44) {
            
              return  44+12+10+3;
              
          } else {
              
             // return  contentH+12+10+3;
              return  contentH+3;
              
          }
            
      
      } else {
            
          
          return contentH+12+10+3;
          
      }
    
        
    
       
    }else if (indexPath.section == 1) {
        
        return  75*CFLScaleValue+6;
        
        
    } else  if(indexPath.section == 2){
        
         return  70+6;
        
    } else if (indexPath.section == 3) {
        
        
       return  58*CFLScaleValue+21+6;
        
    }
    
    else {
        
        return 100;
    }
   
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return  36;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return  0.01;
    
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectinView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 36)];
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, 200, 36)];
    textlabel.text = self.sectinArr[section];
   //  textlabel.backgroundColor = [UIColor redColor];
    textlabel.textColor = RGB(51, 51, 51);
    
    textlabel.font = [UIFont systemFontOfSize:13];
    [sectinView addSubview:textlabel];
    return sectinView;
    
   
  
}




#pragma mark  ------------  计算文字的高度


- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize

{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenW-36, MAXFLOAT)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    return rect.size.height;
    
}






#pragma mark  ------------- UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.dataDic[@"imgs"] count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWJietuCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    NSString *icoStr = self.dataDic[@"imgs"][indexPath.row];
    NSURL *url = [NSURL URLWithString:icoStr];
    [cell.jietuIcoimasgeView sd_setImageWithURL:url];


    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(110*CFLScaleValue,180*CFLScaleValue);
}



//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//
//    DLog(@"点击精品手游单元格");
//}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DLog(@"点击精品手游单元格11111");
    
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    bgView.pagingEnabled = YES;
    self.scrollView = bgView;
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.scrollView.delegate = self;
    NSArray *imagesarr =   self.dataDic[@"imgs"];
    [self.scrollView setContentSize:CGSizeMake( imagesarr.count*SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    
    
      self.scrollView.contentOffset = CGPointMake(indexPath.row * SCREEN_WIDTH, 0);
    
    
       for (int i = 0 ; i <  imagesarr.count; i ++) {
           
//           NSLog(@"youzhima%lu", (unsigned long)self.entity.imgs.count);
          


           UIImageView *imageView = [[UIImageView alloc] init];
           // imageView.image =  [UIImage imageNamed:self.entity.imgs[i]];
           
           // [imageView sd_setImageWithURL:[NSURL URLWithString:self.entity.imgs[i]] placeholderImage:[UIImage imageNamed:@""]];
           // imageView.image = [UIImage imageNamed:@"1"];
           
           imageView.tag = i+100;
           
           // imageView.frame = [bgView convertRect:picView.frame fromView:self.view];
           [bgView addSubview:imageView];
           
           //        CGRect frame = imageView.frame;
           //        frame.size.width = bgView.frame.size.width;
           //        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
           //        frame.origin.x = i *  bgView.frame.size.width;
           //        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
           //        imageView.frame = frame;
           
           
           if (i == indexPath.row) {
          
               
                [imageView  sd_setImageWithURL:[NSURL URLWithString:imagesarr[i]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

                       CGRect frame = imageView.frame;
                       frame.size.width = self.scrollView.frame.size.width;
                       frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
                       frame.origin.x = i *  self.scrollView.frame.size.width;
                       frame.origin.y = (self.scrollView.frame.size.height - frame.size.height) * 0.5;
                       imageView.frame = frame;
                       imageView.image = image;

               //        self.lastImageView =  imagView;
               //
               //        self.originalFrame = imagView.frame;


                   }];


               continue;
           }
           
           
           
       }
    
    
    
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger leftI = scrollView.contentOffset.x / SCREEN_WIDTH;
    UIImageView *imagView = [self.scrollView viewWithTag:leftI+100];
    //  [imagView sd_setImageWithURL:[NSURL URLWithString:self.entity.imgs[i]] placeholderImage:[UIImage imageNamed:@""]];
    
      NSArray *imagesarr =   self.dataDic[@"imgs"];
    
    [imagView  sd_setImageWithURL:[NSURL URLWithString:imagesarr[leftI]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        CGRect frame = imagView.frame;
        frame.size.width = self.scrollView.frame.size.width;
        frame.size.height = frame.size.width * (imagView.image.size.height / imagView.image.size.width);
        frame.origin.x = leftI *  self.scrollView.frame.size.width;
        frame.origin.y = (self.scrollView.frame.size.height - frame.size.height) * 0.5;
        imagView.frame = frame;
        imagView.image = image;
        
//        self.lastImageView =  imagView;
//
//        self.originalFrame = imagView.frame;
        
        
    }];
    
    
    
    
    
    
    
    
    
    
    
    
}










-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    //    self.scrollView.contentOffset = CGPointZero;
    //    [UIView animateWithDuration:0.5 animations:^{
    //        self.lastImageView.frame = self.originalFrame;
    //        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    //    } completion:^(BOOL finished) {
    //        [tapBgRecognizer.view removeFromSuperview];
    //        self.scrollView = nil;
    //        self.lastImageView = nil;
    //    }];
    
    
  
    self.scrollView.contentOffset = CGPointZero;
    
    
    [self.scrollView removeFromSuperview];
    
    
    
    
    
    
    
}






#pragma mark -----------

- (void)pinglunbutton {
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    view.backgroundColor = [UIColor clearColor];
    
    UIView *xiangqin = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    xiangqin.backgroundColor = [UIColor blackColor];
    xiangqin.alpha = 0.6;
    [view addSubview:xiangqin];
    
    
    
    HWyouxixiangqinglimiandepinglunView *addcom = [[HWyouxixiangqinglimiandepinglunView alloc]initWithFrame:CGRectMake(0, 210, kScreenW, (kScreenH-210))];
    addcom.backgroundColor =  RGB(255, 255, 255);
    addcom.layer.cornerRadius = 12;
    addcom.layer.masksToBounds = YES;
    
    [view addSubview:addcom];
    
     addcom.idStr = self.idStr;
    [kKeyWindow addSubview:view];
    
    
    
    
    
}






@end
